<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		
		<script>
			//打开或创建一个数据库
			var request = indexedDB.open("databaseName",1);
			
			//数据库版本改变或首次创建触发
			request.onupgradeneeded = function(event){
				var db = event.target.result;
				
				//创建对象存储表,设置主键为'id'
				var objectStore = db.createObjectStore("customers",{keyPath:'id'});
				
				//为'name'字段创建索引
				objectStore.createIndex("name","name",{unique:false});
			}
			
			//打开数据库成功
			request.onsuccess = function(event){
				var db = event.target.result;
				
				//插入数据
				var transaction = db.transaction(['customers'],"readwrite");
				var objectStore = transaction.objectStore('customers');
				objectStore.add({id:1,name:"11111",email:"22871501554@qq.com"})
				objectStore.add({id:2,name:"22222",email:"22871501554@qq.com"})
				
				transaction.oncomplete = function(){
					console.log("Transaction completed: data added.")
				}
				
				transaction.onerror = function(event){
					console.error('Transaction failed:', event);
				}
				
				//查询数据
				var queryTransaction = db.transaction(['customers']);
				var queryObjectStore = queryTransaction.objectStore(['customers']);
				var query = queryObjectStore.get(1);
				
				query.onsuccess = function(event){
					console.log('Customer:', event.target.result)
				}
				
				//更新数据
				var updateTransaction = db.transaction(['customers'],"readwrite");
				var updateObjectStore = updateTransaction.objectStore(['customers']);
				var updatedCustomer  = {id:1,name:"sadsa",email:"454s54q54sq"};
				
				updateObjectStore.put(updatedCustomer);
				
				updateTransaction.oncomplete = function(){
					console.log('Transaction completed: data updated.');
				}
			}
			
			//处理错误
			request.onerror = function(event){
				console.log('Database error:', event.target.error);
			}
		</script>
	</body>
</html>